// pages/work/add/add.js
const utils = require('../../../utils/activity_util')
const util = require("../../../utils/util")
const my = require("../../../utils/MyUtils")
const datas = require("../../../data/data")
const log=require("../../../utils/log")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    qjType: datas.qjType,
    nowlen: 0,
    length: 200,
    status1: true,
    status2: true,
    changefalg: false,
    // changefalg1: false,
    value: [0, 1, 1, 1, 1],
    openflag: true, //1日期控件显示  2控件滚动选择 底部页面不滚动
    openflag1: true,
    years: [], //时间可选范围模态框数据
    months: [],
    days: [],
    hours: [],
    minutes: [],
    year: '', //时间值
    month: '',
    day: '',
    hour: '',
    minute: '',
    starttime: utils.getobjDate(),
    list: [],
    biaodan: {
      lx: 0, //类型
      start: null, //开始时间
      end: null, //结束时间
      num: 0, //天数
      info: null, //事由
      spr: '', //审批人
      csr: '' //抄送人
    },
    buttonStuts: false,
    tabScrollTop: 0,
    dept: null,
    csname: '',
    bgColor: ''
  },
  listenerPickerSelected: function (e) {
    //改变index值，通过setData()方法重绘界面
    var biaodan = this.data.biaodan
    biaodan.lx = e.detail.value
    this.setData({
      biaodan: biaodan
    });
  },
  getTabScrollTop: function (e) {
    this.setData({
      tabScrollTop: e.detail.y,
    })
  },
  //监听用户滑动页面事件
  onPageScroll: function (e) {
    var buttonStutes = false
    if (e.scrollTop > this.data.tabScrollTop) {
      // 滚动到最底部
      e.scrollTop = 0;
      buttonStutes = true;
    } else {
      e.scrollTop = 0;
      buttonStutes = false;
    }
    //给scrollTop重新赋值 
    this.setData({
      scrollTop: e.scrollTop,
      buttonStuts: buttonStutes
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var datas=my.testMy()
    this.setData({
      years:datas.years,
      months:datas.months,
      days:datas.days,
      hours:datas.hours,
      minutes:datas.minutes
    })
    var t = this
    var _TOKEN = wx.getStorageSync('_TOKEN')
    my.request("/oa-applet/initiate-application", "get", {}, {
      token: _TOKEN
    }, function (res) {
      wx.hideLoading()
      t.setData({
        dept: res.data.data.department_name
      })
    })
  },

  /**
   * 查看补卡登记页面
   */
  bk: function () {
    var that = this
    if (!that.data.status1) {
      that.setData({
        status1: !this.data.status1,
      })
    }
  },
  /**
   * 查看补卡申请
   */
  ck: function () {
    var that = this
    var list = this.data.list
    var _TOKEN = wx.getStorageSync('_TOKEN')
    my.request("/oa-applet/application-record", "get", {
      apply_type: '1'
    }, {
      token: _TOKEN
    }, function (res) {
      wx.hideLoading()
      if (res.data.code == 0) {
        list = res.data.data
        for (var i = 0; i < list.length; i++) {
          list[i].ycType = 1
          list[i].shrq = list[i].start_time.substr(0, 10)
        }
        that.setData({
          list
        })
      } else {
        my.alert(res.data.message)
      }
    })
    var stat = true
    var stat1 = false
    if (this.data.status1 == false) {
      stat1 = false
    }
    if (this.data.status2 == false) {
      stat = true
    }
    this.setData({
      status1: stat1,
      status2: stat
    })
  },
  //  点击日期组件确定事件  
  bindDateChange: function (e) {
    this.setData({
      dates: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      times: e.detail.value
    })
  },
  tap(ev) {
    // 根据选择项目  传去对应数据  根据开始结束时间获取索引  设置面板默认数据
    var flag = ev.currentTarget.dataset.flag
    let value = [2019, 0, 0, 0, 0];
    let arr = [];
    arr = utils.getarrWithtime(this.data.starttime);
    const {
      years,
      months,
      days,
      hours,
      minutes,
      openflag
    } = this.data;
    //根据arr  数据索引
    // console.log(arr)
    value[0] = years.indexOf(arr[0] + '年');
    value[1] = months.indexOf(arr[1] + '月');
    value[2] = days.indexOf(arr[2] + '日');
    value[3] = hours.indexOf(arr[3]);
    value[4] = minutes.indexOf(arr[4]);
    if (flag == "start") {
      this.setData({
        value,
        openflag: false,
        years, //日期模态框数据
        months,
        days,
        hours,
        minutes,
      })
    } else {
      this.setData({
        value,
        openflag1: false,
        years, //日期模态框数据
        months,
        days,
        hours,
        minutes,
      })
    }

  },
  // 取消
  canslebtn(e) {
    var openflag = true
    var openflag1 = true
    if (e.currentTarget.dataset.flag == 'start') {
      var openflag = true
    } else {
      openflag1 = true
    }
    this.setData({
      openflag,
      openflag1,
      changefalg: false,
    })
  },
  // 确定  如果不选择那么默认重置
  closebtn(ev) {
    var flag = ev.currentTarget.dataset.flag
    var biaodan = this.data.biaodan
    this.setData({
      openflag: true,
      openflag1: true,
      changefalg: true,
    })
    const {
      curindex,
      year,
      month,
      day,
      hour,
      minute
    } = this.data;
    debugger
    if (this.data.changefalg) {
      if (flag == "start") {
        if (biaodan.end == null) {
          biaodan.start = utils.getDate(year, month, day, hour, minute)
        } else {
          var end = new Date(biaodan.end).valueOf()
          var start = new Date(utils.getDate(year, month, day, hour, minute)).valueOf()
          if (start > end) {
            my.alert("结束时间不能小于开始时间")
            biaodan.start = null
          } else {
            biaodan.start = utils.getDate(year, month, day, hour, minute)
          }
        }
      } else {
        var start = new Date(biaodan.start).valueOf()
        var end = new Date(utils.getDate(year, month, day, hour, minute)).valueOf()
        if (start > end) {
          my.alert("结束时间不能小于开始时间")
          biaodan.end = null
        } else {
          biaodan.end = utils.getDate(year, month, day, hour, minute)
        }
      }
      this.setData({
        biaodan,
        changefalg: false,
      })
    }
  },
  bindChange: function (ev) {
    var flag = ev.currentTarget.dataset.flag
    const e = ev;
    let val = e.detail.value;
    const year = this.data.years[val[0]];
    const month = this.data.months[val[1]];
    const day = this.data.days[val[2]];
    const hour = this.data.hours[val[3]];
    const minute = this.data.minutes[val[4]];
    //如果点击月份  那么后面日跟着变换数据
    let days = [];
    const daynum = utils.mGetDate(year.substr(0, year.length - 1), month.substr(0, month.length - 1));
    for (let i = 1; i <= daynum; i++) {
      days.push(i + "日")
      days[i-1]=days[i-1].padStart(3,"0")
    }
    if (flag == "start") {
      this.setData({
        days,
        year,
        month,
        day,
        hour,
        minute,
        changefalg: true,
        start: this.data.starttime,
      })
    } else {
      this.setData({
        days,
        year,
        month,
        day,
        hour,
        minute,
        changefalg: true,
        end: this.data.starttime
      })
    }
  },
  /**
   * 获取事由字数
   * @param {*} res 
   */
  getLength(res) {
    var n = res.detail.value;
    var len = n.length;
    var biaodan = this.data.biaodan
    biaodan.info = n
    this.setData({
      nowlen: len,
      biaodan
    })
  },
  onShow: function () {
    var biaodan = this.data.biaodan
    if (Object.keys(app.globalData.csr).length != 0) {
      biaodan.csr = app.globalData.csr.uid
      this.setData({
        biaodan,
        csrName: app.globalData.csr.use_name
      })
    }
    if (Object.keys(app.globalData.spr).length != 0) {
      biaodan.spr = app.globalData.spr.uid
      this.setData({
        biaodan,
        sprName: app.globalData.spr.use_name
      })
    }
  },
  spr(e) {
    wx.navigateTo({
      url: '../list/index?flag=sp',
    })
  },
  cs(e) {
    wx.navigateTo({
      url: '../list/index?flag=cs',
    })
  },
  submit() {
    var t = this
    var _TOKEN = wx.getStorageSync('_TOKEN')
    var biaodan = this.data.biaodan
    biaodan.lx == 0 ? my.alert("类型不能为空") : (
      biaodan.start == null && biaodan.start == '' ? my.alert("开始时间为空") : (
        biaodan.end == null && biaodan.end == '' ? my.alert("结束时间为空") : (
          biaodan.num == 0 ? my.alert("请假天数为空") : (
            biaodan.info == '' || biaodan.info == null ? my.alert("出差事由为空") : (
              biaodan.spr == '' && biaodan.spr == '' ? my.alert("审批人为空") : my.request("/oa-applet/initiate-application-func?apply_type=1", "post",
                my.json2Form({
                  leave_type: biaodan.lx,
                  leave_start_time: biaodan.start,
                  leave_end_time: biaodan.end,
                  leave_num: biaodan.num,
                  leave_reason: biaodan.info,
                  leave_approver_uid: biaodan.spr,
                  leave_copyer_uid: biaodan.csr
                }), {
                  token: _TOKEN,
                  'content-type': 'application/x-www-form-urlencoded',
                  'accept': 'application/json'
                },
                function (res) {
                  wx.hideLoading()
                  log.info(my.info(new Date(),wx.getStorageSync('userInfo').user_name,"提交了请假申请"))
                  my.alert(res.data.message)
                  biaodan.start = ""
                  biaodan.end = ""
                  biaodan.num = ''
                  biaodan.lx = 0
                  biaodan.info = ""
                  biaodan.spr = ""
                  biaodan.csr = ""
                  app.globalData.csr = {}
                  app.globalData.spr = {}
                  t.setData({
                    biaodan,
                    sprName:"",
                    csrName:""
                  })
                })
            )
          )
        )
      )
    )
  },
  num(e) {
    var biaodan = this.data.biaodan
    if (e.detail.value) {
      biaodan.num = e.detail.value
    } else {
      biaodan.num = ''
    }
    this.setData({
      biaodan
    })
  },
  selectOne(e) {
    wx.navigateTo({
      url: '../xaingqing/index?apply_type=1&apply_id='+e.currentTarget.dataset.id,
    })
  },
  onUnload: function () {
    app.globalData.csr = {}
    app.globalData.spr = {}
  }
})